package livesun.justiceeternal.db.task;


import org.greenrobot.greendao.database.Database;

import livesun.justiceeternal.base.JEApp;
import livesun.justiceeternal.db.DaoMaster;
import livesun.justiceeternal.db.DaoSession;
import livesun.justiceeternal.utils.HandlerFactory;

import static livesun.justiceeternal.db.DaoMaster.createAllTables;


/**
 * Author: livesun
 * Date: 2018/8/20 17:57
 * Description:
 */
public class DbScheduler {
    private static DaoSession sSession;

    static DaoSession getSession() {
        if (sSession == null) {
            synchronized (DbScheduler.class) {
                if (sSession == null) {
//                    if (BuildConfig.LOG_ENABLE) {
//                        initDebug();
//                    } else {
                    initRelease();
//                    }
                }
            }
        }

        return sSession;
    }


    private static void initRelease() {
        DaoMaster.OpenHelper helper = new DaoMaster.OpenHelper(JEApp.getInstance(), "note_db") {

            @Override
            public void onCreate(Database db) {
                super.onCreate(db);
            }
            @Override
            public void onUpgrade(Database db, int oldVersion, int newVersion) {
                // todo 处理升级问题, 包括增减字段等
                createAllTables(db,true);
            }
        };
        sSession = new DaoMaster(helper.getWritableDb()).newSession();
    }

    public static void submit(AbsDbTask runnable) {
        HandlerFactory.db().post(runnable);
    }
}
